<%
# encoding: utf-8
#
# Redmine plugin for Document Management System "Features"
#
# Copyright © 2011-20 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<h3 class="title"><%= l(:title_create_link) %></h3>

<%= labelled_form_for @dmsf_link, :remote => modal do |f| %>
  <%= error_messages_for @dmsf_link %>
  <%= f.hidden_field :project_id, :value => @dmsf_link.project_id %>
  <%= f.hidden_field :dmsf_folder_id, :value => @dmsf_link.dmsf_folder_id if @dmsf_link.dmsf_folder_id %>
  <%= f.hidden_field :type, :value => @type %>
  <%= f.hidden_field :dmsf_file_id, :value => @dmsf_file_id %>
  <%= f.hidden_field(:container, :value => @container) if @container %>
  <div class="box tabular">
    <% if (@type == 'link_from') && !@container %>
      <p>
        <%= radio_button_tag(:external_link, 'false', true) %> <%= l(:label_internal) %><br/>
        <%= radio_button_tag(:external_link, 'true', false) %>  <%= l(:label_external) %>
      </p>
    <% end %>
    <div id="dmsf_link_internal">
      <% unless @fast_links && (@type == 'link_to') %>
        <p>
          <% if @type == 'link_from' %>
            <label for="dmsf_link[target_project_id]"><%= l(:label_source_project) %></label>
          <% else %>
            <label for="dmsf_link[target_project_id]"><%= l(:label_target_project) %></label>
          <% end %>
          <%= select_tag('dmsf_link[target_project_id]',
            project_tree_options_for_select(DmsfFile.allowed_target_projects_on_copy,
            :selected => @dmsf_link.target_project)) %>
          <%= late_javascript_tag do %>
              $('#dmsf_link_target_project_id').change(function(){
                $.ajax({
                  url: '<%= escape_javascript autocomplete_for_project_dmsf_link_path(@project, :format => 'js') %>',
                  type: 'get',
                  data: $('#new_dmsf_link').serialize()
                });
              });
          <% end %>
        </p>
      <% end %>
      <p>
        <% if @type == 'link_from' %>
          <%= label_tag('dmsf_link[target_folder_id]', l(:label_source_folder)) %>
        <% else %>
          <label for="dmsf_link_target_folder_id">
            <%= l(:label_target_folder) %> <%= ' #' if @fast_links %>
            <span class="required">*</span>
          </label>
          <% if @fast_links %>
            <%= text_field_tag 'dmsf_link[target_folder_id]', '', :required => true, :max_length => 255 %>
          <% end %>
        <% end %>
        <% if !@fast_links || (@type != 'link_to') %>
          <%= select_tag('dmsf_link[target_folder_id]',
            folder_tree_options_for_select(DmsfFolder.directory_tree(@dmsf_link.target_project),
            :selected => @target_folder_id)) %>
          <%= late_javascript_tag do %>
              $('#dmsf_link_target_folder_id').change(function(){
                $.ajax({
                  url: '<%= escape_javascript autocomplete_for_folder_dmsf_link_path(@project, :format => 'js') %>',
                  type: 'get',
                  data: $('#new_dmsf_link').serialize()
                });
              });
          <% end %>
        <% end %>
      </p>
      <% if @type == 'link_from' %>
        <p>
          <%= label_tag('dmsf_link[target_file_id]', l(:field_target_file)) %>
          <% files = files_for_select(@dmsf_link.target_project.id, @target_folder_id) %>
          <%= select_tag('dmsf_link[target_file_id]',
            options_for_select(DmsfFolder.file_list(files)), :required => modal) %>
        </p>
      <% end %>
    </div>
    <% if (@type == 'link_from') && !@container %>
      <div id="dmsf_link_external">
        <p>
          <%= f.text_field :external_url, :required => false %>
        </p>
      </div>
    <% end %>
    <p>
      <%= f.text_field :name, :required => true, :max_length => 255 %>
    </p>
  </div>
  <p>
    <% if modal %>
        <%= f.submit l(:button_create), :onclick => 'hideModal(this);' %>
    <% else %>
        <%= f.submit l(:button_create) %>
    <% end %>
  </p>
<% end %>

<%= late_javascript_tag do %>
  <%# Select2 extension, TODO: in case of a modal window, select2 makes problems %>
  <% unless modal || @fast_links %>
    $('#dmsf_link_target_project_id').select2();
    $('#dmsf_link_target_folder_id').select2();
    $('#dmsf_link_target_file_id').select2();
  <% end %>
  <%# Suggest a link's name when a file is selected %>
  $('#dmsf_link_target_file_id').change(function () {
    var linkName = $('#dmsf_link_name');
    var name = linkName.val();
    var dirName = $('#dmsf_link_target_folder_id option:selected').text().replace(/\./g, '')
    var fileName = $('#dmsf_link_target_file_id option:selected').text().replace(/\./g, '')
    if((name == '') || name == dirName) {
      linkName.val(fileName);
    }
  });
  <%# Suggest a link's name when a folder is selected %>
  $('#dmsf_link_target_folder_id').change(function () {
    var linkName = $('#dmsf_link_name');
    var name = linkName.val();
    var dirName = $('#dmsf_link_target_folder_id option:selected').text().replace(/\./g, '')
    if(name == '') {
      linkName.val(dirName);
    }
  });
  <%# Internal/External link switch %>
  $("input[name=external_link]:radio").change(function(){
    $("#dmsf_link_internal").toggle();
    $("#dmsf_link_external").toggle();
    $("#dmsf_link_external_url").toggleClass('required', $(this).val());
    var labelUrl = $('label[for="dmsf_link_external_url"]');
    labelUrl.toggleClass('required', $(this).val());
    if(labelUrl.find(".required").length == 0){
      labelUrl.append('<span class="required"> *</span>');
    }
  });
<% end %>
